package com.rfzn.swipingcard

import android.annotation.SuppressLint
import android.app.Application
import android.util.Log
import me.f1reking.serialportlib.SerialPortHelper
import me.f1reking.serialportlib.entity.DATAB
import me.f1reking.serialportlib.entity.FLOWCON
import me.f1reking.serialportlib.entity.PARITY
import me.f1reking.serialportlib.entity.STOPB
import me.f1reking.serialportlib.listener.IOpenSerialPortListener
import me.f1reking.serialportlib.listener.ISerialPortDataListener
import me.f1reking.serialportlib.listener.Status
import java.io.File
import java.util.*
import kotlin.concurrent.schedule

class App:Application() {
    companion object{
        private const val TAG = "App"
        //刷卡串口串口名称和波特率
        const val SP_NAME_SWIPING_CARD = "ttyS1"
        const val SP_RATE_SWIPING_CARD = 9600

        //定义刷卡串口
        lateinit var swipingCardSerialPortHelper: SerialPortHelper

        //刷卡串口回调
        private lateinit var onSwipingCardListener: ((swipingCard: String) -> Unit)
        fun setOnSwipingCardListener(onSwipingCardListener: ((swipingCard: String) -> Unit)) {
            this.onSwipingCardListener = onSwipingCardListener
        }
    }

    init {
        Timer().schedule(100){
            openSwipingCard()
        }
    }

    //打开刷卡串口
    private fun openSwipingCard() {
        swipingCardSerialPortHelper = SerialPortHelper()
        swipingCardSerialPortHelper.port = "/dev/$SP_NAME_SWIPING_CARD"
        swipingCardSerialPortHelper.baudRate = SP_RATE_SWIPING_CARD
        swipingCardSerialPortHelper.stopBits = STOPB.getStopBit(STOPB.B2)
        swipingCardSerialPortHelper.dataBits = DATAB.getDataBit(DATAB.CS8)
        swipingCardSerialPortHelper.parity = PARITY.getParity(PARITY.NONE)
        swipingCardSerialPortHelper.flowCon = FLOWCON.getFlowCon(FLOWCON.NONE)
        swipingCardSerialPortHelper.setIOpenSerialPortListener(object : IOpenSerialPortListener {
            override fun onSuccess(device: File) {
                Log.d(TAG, "刷卡串口:串口打开成功")
            }

            override fun onFail(device: File, status: Status) {
                when (status) {
                    Status.NO_READ_WRITE_PERMISSION -> {
                        Log.d(TAG, "刷卡串口：没有读写权限")
                    }
                    Status.OPEN_FAIL -> {
                        Log.d(TAG, "刷卡串口：串口打开失败")
                    }
                    else -> {
                        Log.d(TAG, "刷卡串口：串口打开失败")
                    }
                }
            }

        })

        swipingCardSerialPortHelper.setISerialPortDataListener(object :
            ISerialPortDataListener {
            override fun onDataReceived(bytes: ByteArray) {
                val icCardNumber=String(bytes)
                Log.d(TAG, "刷卡串口：接收----${icCardNumber},size=${icCardNumber.length}")

                val activity=AcitivityManager.instance.getTopActivity()
                if (activity is LoginActivity)
                {
                    onSwipingCardListener.invoke(icCardNumber)
                }

            }

            override fun onDataSend(bytes: ByteArray) {
                val send=String(bytes)
                Log.d(TAG, "刷卡串口：发送----${send},size=${send.length}")

            }

        })

        //打开
        swipingCardSerialPortHelper.open()

    }


}